/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.commons.utils;

/**
 * StringUtils
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class StringUtils {
	
	public static final String EMPTY = "";
	
	/**
	 * @param s String to be examined
	 * @return true if the given String is either null 
	 * or it is empty based on {@link String#isEmpty()} call.
	 */
	public static boolean isNullOrEmpty(String s) {
		return s == null || s.isEmpty();
	}
	
	/**
	 * @param str String to be checked and converted if needed
	 * @return original value if the string wasn't null, empty String otherwise
	 */
	public static String getEmptyIfNull(String str) {
        return toString(str); 
    }
    
	/**
	 * Converts an Object to String
	 * @param o an Object to be converted
	 * @return if o == null returns empty String, otherwise 
	 * o is converted to string using {@link Object#toString()} method
	 */
    public static String toString(Object o) {
    	if (o == null) return EMPTY;
    	return o.toString();
    }
    
    /**
     * @param str String to be examined and converted if needed
     * @return if the given String is not null or empty regarding to the logic
     * of {@link #isNullOrEmpty(String)} method, it is returned without any change.
     * Otherwise null is returned
     */
    public static String getNullIfEmpty(String str) {
    	if (isNullOrEmpty(str)) return null;
    	return str;
    }

}

